<?php
/**
 * ECSHOP 控制台首页
 *
 * @author iceman
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

/*------------------------------------------------------ */
//-- 列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
    /* 权限检查 */
    admin_priv('index_customize');

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG['01_index_customize']);
    $smarty->assign('action_link',  array('href' => 'index_mgt.php?act=add_top', 'text' => $_LANG['add_top']));
    $smarty->assign('full_page',    1);

    $smarty->assign('all_cat',     get_all_cat());

    /* 列表页面 */
    assign_query_info();
    $smarty->display('index_mgt_list.htm');
}

/*------------------------------------------------------ */
//-- 新增或修改顶级分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add_top' || $_REQUEST['act'] == 'edit_top')
{
    /* 权限检查 */
    admin_priv('index_customize');

    $cat_id = intval($_REQUEST['cat_id']);
    $cat_info = empty($cat_id) ? array('level' => 'top') : get_cat($cat_id);

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG[$_REQUEST['act']]);
    $smarty->assign('action_link',  array('href' => 'index_mgt.php?act=list', 'text' => $_LANG['list_top']));

    $smarty->assign('cat_info',     $cat_info);
    $smarty->assign('form_act',     'update_cat');

    /* 显示页面 */
    assign_query_info();
    $smarty->display('index_mgt_cat.htm');
}

/*------------------------------------------------------ */
//-- 新增或修改分类
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add_cat' || $_REQUEST['act'] == 'edit_cat')
{
    /* 权限检查 */
    admin_priv('index_customize');

    $cat_id = intval($_REQUEST['cat_id']);
    $cat_info = empty($cat_id) ? array('level' => 'cat') : get_cat($cat_id);

    $parent_info = get_top_cat();

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG[$_REQUEST['act']]);
    $smarty->assign('action_link',  array('href' => 'index_mgt.php?act=list', 'text' => $_LANG['list_top']));

    $smarty->assign('cat_info',     $cat_info);
    $smarty->assign('parent_info',  $parent_info);
    $smarty->assign('form_act',     'update_cat');

    /* 显示页面 */
    assign_query_info();
    $smarty->display('index_mgt_cat.htm');
}

elseif($_REQUEST['act'] == 'update_cat')
{
    $cat_id = intval($_POST['inc_id']);

    $cat_info = array();
    $cat_info['parent_id'] = empty($_POST['parent_id']) ? 0 : intval($_POST['parent_id']);
    if(!empty($_POST['inc_name'])) $cat_info['inc_name'] = $_POST['inc_name'];
    if(!empty($_POST['inc_code'])) $cat_info['inc_code'] = $_POST['inc_code'];
    if(!empty($_POST['level'])) $cat_info['level'] = $_POST['level'];
    if(!empty($_POST['sort_order'])) $cat_info['sort_order'] = $_POST['sort_order'];

    if($cat_id)
    {
        $res = $db->autoExecute($ecs->table('index_customize'), $cat_info, "UPDATE", "inc_id='$cat_id'");
    }
    else
    {
        $res = $db->autoExecute($ecs->table('index_customize'), $cat_info);

        $cat_id = $db->insert_id();
    }
    if($res == false)
    {
        $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
        sys_msg($_LANG['op_fail'], 0, $link);
    }

    $cat_info['arr_parent_id'] = '00000000,'.str_pad($cat_id, 8, '0', STR_PAD_LEFT);
    if($cat_info['parent_id'])
    {
        $parent_info = get_cat($cat_info['parent_id']);
        if(!empty($parent_info))
        {
            $cat_info['arr_parent_id'] = $parent_info['arr_parent_id'].','.str_pad($cat_id, 8, '0', STR_PAD_LEFT);
        }
    }
    $db->autoExecute($ecs->table('index_customize'), array('arr_parent_id' => $cat_info['arr_parent_id']), "UPDATE", "inc_id='$cat_id'");

    /* 提示信息 */
    $link[] = array('text' => $_LANG['back_list_top'], 'href' => 'index_mgt.php?act=list');
    sys_msg($_LANG['attradd_succed'], 0, $link);
}

/*------------------------------------------------------ */
//-- 新增或修改详情
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'add_detail' || $_REQUEST['act'] == 'edit_detail')
{
    /* 权限检查 */
    admin_priv('index_customize');

    $cat_id = intval($_REQUEST['cat_id']);
    if(empty($cat_id))
    {
        $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
        sys_msg($_LANG['no_cat'], 0, $link);
    }
    $cat_info = get_cat($cat_id);

    $detail_id = intval($_REQUEST['detail_id']);
    $detail_info = empty($detail_id) ? array('level'=>'detail', 'parent_id'=>$cat_id) : get_cat($detail_id);

    $types = get_types();

    /* 模板赋值 */
    $smarty->assign('ur_here',      $_LANG[$_REQUEST['act']]);
    $smarty->assign('action_link',  array('href' => 'index_mgt.php?act=list', 'text' => $_LANG['list_top']));

    $smarty->assign('cat_info',     $cat_info);
    $smarty->assign('detail_info',  $detail_info);
    $smarty->assign('types',        $types);
    $smarty->assign('form_act',     'update_detail');

    /* 显示页面 */
    assign_query_info();
    $smarty->display('index_mgt_detail.htm');
}

elseif($_REQUEST['act'] == 'update_detail')
{
    $cat_id = intval($_POST['inc_id']);

    $cat_info = array();
    $cat_info['parent_id'] = empty($_POST['parent_id']) ? 0 : intval($_POST['parent_id']);
    if(!empty($_POST['inc_name'])) $cat_info['inc_name'] = $_POST['inc_name'];
    if(!empty($_POST['inc_code'])) $cat_info['inc_code'] = $_POST['inc_code'];
    if(!empty($_POST['level'])) $cat_info['level'] = $_POST['level'];
    if(!empty($_POST['sort_order'])) $cat_info['sort_order'] = $_POST['sort_order'];

    if($cat_id)
    {
        $res = $db->autoExecute($ecs->table('index_customize'), $cat_info, "UPDATE", "inc_id='$cat_id'");
    }
    else
    {
        $res = $db->autoExecute($ecs->table('index_customize'), $cat_info);

        $cat_id = $db->insert_id();
    }
    if($res == false)
    {
        $link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
        sys_msg($_LANG['op_fail'], 0, $link);
    }

    $cat_info['arr_parent_id'] = '00000000,'.str_pad($cat_id, 8, '0', STR_PAD_LEFT);
    if($cat_info['parent_id'])
    {
        $parent_info = get_cat($cat_info['parent_id']);
        if(!empty($parent_info))
        {
            $cat_info['arr_parent_id'] = $parent_info['arr_parent_id'].','.str_pad($cat_id, 8, '0', STR_PAD_LEFT);
        }
    }
    $db->autoExecute($ecs->table('index_customize'), array('arr_parent_id' => $cat_info['arr_parent_id']), "UPDATE", "inc_id='$cat_id'");

    /* 提示信息 */
    $link[] = array('text' => $_LANG['back_list_top'], 'href' => 'index_mgt.php?act=list');
    sys_msg($_LANG['attradd_succed'], 0, $link);
}

/*------------------------------------------------------ */
//-- 动态变更
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'edit_cat_name')
{
    check_authz_json('index_customize');

    $id = intval($_POST['id']);
    $val = $_POST['val'];

    if (content_update($id, array('inc_name' => $val)) != false)
    {
        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}
if ($_REQUEST['act'] == 'edit_cat_code')
{
    check_authz_json('index_customize');

    $id = intval($_POST['id']);
    $val = $_POST['val'];

    if (content_update($id, array('inc_code' => $val)) != false)
    {
        make_json_result($val);
    }
    else
    {
        make_json_error($db->error());
    }
}


//更新分类
function content_update($cat_id, $args)
{
    if (empty($args) || empty($cat_id))
    {
        return false;
    }

    return $GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('index_customize'), $args, 'update', "inc_id='$cat_id'");
}

//获得类比数组
function get_types()
{
    global $_LANG;

    return array(
        'text' => $_LANG['detail_type_text'],
        'textlink' => $_LANG['detail_type_textlink'],
        'pic' => $_LANG['detail_type_pic'],
        'piclink' => $_LANG['detail_type_piclink'],
    );
}


//获得顶级分类
function get_top_cat()
{
    global $_LANG;

    $list = array();

    $sql = "SELECT * ".
           " FROM " . $GLOBALS['ecs']->table('index_customize').
           " WHERE level <> 'detail' order by arr_parent_id asc, sort_order asc";
    $arr = $GLOBALS['db']->getAll($sql);
    if(!empty($arr))
    {
        foreach($arr as $v)
        {
            $v['arr_parent_id'] = explode(',', $v['arr_parent_id']);
            $v['inc_name_level'] = str_repeat('---', count($v['arr_parent_id'])-2);

            $list[$v['inc_id']] = $v;
        }
    }

    return $list;
}

//获得记录
function get_cat($id=0)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('index_customize'). " WHERE inc_id='$id' LIMIT 1";
    return $GLOBALS['db']->getRow($sql);
}

//获得所有类别
function get_all_cat()
{
    global $_LANG;

    $cats = array();

    $sql = "select * from " . $GLOBALS['ecs']->table('index_customize'). " order by arr_parent_id asc";
    $arr = $GLOBALS['db']->getAll($sql);
    if(!empty($arr))
    {
        foreach($arr as $v)
        {
            $v['arr_parent_id'] = explode(',', $v['arr_parent_id']);
            $v['inc_name_level'] = str_repeat('---', count($v['arr_parent_id'])-2);

            $v['inc_level_name'] = $_LANG['level_'.$v['level']];

            $cats[$v['inc_id']] = $v;
        }
    }

    return $cats;
}

//获得内容
function get_contents($parent=0)
{
    if(empty($parent))  return array();

    $list = array();

    $sql = "SELECT * ".
           " FROM " . $GLOBALS['ecs']->table('index_customize').
           " WHERE parent_id = ".intval($parent)." order by sort_order asc ";
    $arr = $GLOBALS['db']->getAll($sql);
    if(!empty($arr))
    {
        foreach($arr as $v)
        {
            $list[$v['ic_id']] = $v;
        }
    }

    return $list;
}

?>
